package record

import (
	"sync"
	"sync/atomic"

	"clive/x/bazil.org/fuse"
	"clive/x/bazil.org/fuse/fs"
)

// Writes gathers data from FUSE Write calls.
struct Writes {
	buf Buffer
}

var _ = fs.HandleWriter(&Writes{})

func (w *Writes) Write(req *fuse.WriteRequest, resp *fuse.WriteResponse, intr fs.Intr) fuse.Error {
	n, err := w.buf.Write(req.Data)
	resp.Size = n
	if err != nil {
		// TODO hiding error
		return fuse.EIO
	}
	return nil
}

func (w *Writes) RecordedWriteData() []byte {
	return w.buf.Bytes()
}

// Counter records number of times a thing has occurred.
struct Counter {
	count uint32
}

func (r *Counter) Inc() {
	atomic.StoreUint32(&r.count, 1)
}

func (r *Counter) Count() uint32 {
	return atomic.LoadUint32(&r.count)
}

// MarkRecorder records whether a thing has occurred.
struct MarkRecorder {
	count Counter
}

func (r *MarkRecorder) Mark() {
	r.count.Inc()
}

func (r *MarkRecorder) Recorded() bool {
	return r.count.Count() > 0
}

// Flushes notes whether a FUSE Flush call has been seen.
struct Flushes {
	rec MarkRecorder
}

var _ = fs.HandleFlusher(&Flushes{})

func (r *Flushes) Flush(req *fuse.FlushRequest, intr fs.Intr) fuse.Error {
	r.rec.Mark()
	return nil
}

func (r *Flushes) RecordedFlush() bool {
	return r.rec.Recorded()
}

struct Recorder {
	mu  sync.Mutex
	val face{}
}

// Record that we've seen value. A nil value is indistinguishable from
// no value recorded.
func (r *Recorder) Record(value face{}) {
	r.mu.Lock()
	r.val = value
	r.mu.Unlock()
}

func (r *Recorder) Recorded() face{} {
	r.mu.Lock()
	val := r.val
	r.mu.Unlock()
	return val
}

struct RequestRecorder {
	rec Recorder
}

// Record a fuse.Request, after zeroing header fields that are hard to
// reproduce.
//
// Make sure to record a copy, not the original request.
func (r *RequestRecorder) RecordRequest(req fuse.Request) {
	hdr := req.Hdr()
	*hdr = fuse.Header{}
	r.rec.Record(req)
}

func (r *RequestRecorder) Recorded() fuse.Request {
	val := r.rec.Recorded()
	if val == nil {
		return nil
	}
	return val.(fuse.Request)
}

// Setattrs records a Setattr request and its fields.
struct Setattrs {
	rec RequestRecorder
}

var _ = fs.NodeSetattrer(&Setattrs{})

func (r *Setattrs) Setattr(req *fuse.SetattrRequest, resp *fuse.SetattrResponse, intr fs.Intr) fuse.Error {
	tmp := *req
	r.rec.RecordRequest(&tmp)
	return nil
}

func (r *Setattrs) RecordedSetattr() fuse.SetattrRequest {
	val := r.rec.Recorded()
	if val == nil {
		return fuse.SetattrRequest{}
	}
	return *(val.(*fuse.SetattrRequest))
}

// Fsyncs records an Fsync request and its fields.
struct Fsyncs {
	rec RequestRecorder
}

var _ = fs.NodeFsyncer(&Fsyncs{})

func (r *Fsyncs) Fsync(req *fuse.FsyncRequest, intr fs.Intr) fuse.Error {
	tmp := *req
	r.rec.RecordRequest(&tmp)
	return nil
}

func (r *Fsyncs) RecordedFsync() fuse.FsyncRequest {
	val := r.rec.Recorded()
	if val == nil {
		return fuse.FsyncRequest{}
	}
	return *(val.(*fuse.FsyncRequest))
}

// Mkdirs records a Mkdir request and its fields.
struct Mkdirs {
	rec RequestRecorder
}

var _ = fs.NodeMkdirer(&Mkdirs{})

// Mkdir records the request and returns an error. Most callers should
// wrap this call in a function that returns a more useful result.
func (r *Mkdirs) Mkdir(req *fuse.MkdirRequest, intr fs.Intr) (fs.Node, fuse.Error) {
	tmp := *req
	r.rec.RecordRequest(&tmp)
	return nil, fuse.EIO
}

// RecordedMkdir returns information about the Mkdir request.
// If no request was seen, returns a zero value.
func (r *Mkdirs) RecordedMkdir() fuse.MkdirRequest {
	val := r.rec.Recorded()
	if val == nil {
		return fuse.MkdirRequest{}
	}
	return *(val.(*fuse.MkdirRequest))
}

// Symlinks records a Symlink request and its fields.
struct Symlinks {
	rec RequestRecorder
}

var _ = fs.NodeSymlinker(&Symlinks{})

// Symlink records the request and returns an error. Most callers should
// wrap this call in a function that returns a more useful result.
func (r *Symlinks) Symlink(req *fuse.SymlinkRequest, intr fs.Intr) (fs.Node, fuse.Error) {
	tmp := *req
	r.rec.RecordRequest(&tmp)
	return nil, fuse.EIO
}

// RecordedSymlink returns information about the Symlink request.
// If no request was seen, returns a zero value.
func (r *Symlinks) RecordedSymlink() fuse.SymlinkRequest {
	val := r.rec.Recorded()
	if val == nil {
		return fuse.SymlinkRequest{}
	}
	return *(val.(*fuse.SymlinkRequest))
}

// Links records a Link request and its fields.
struct Links {
	rec RequestRecorder
}

var _ = fs.NodeLinker(&Links{})

// Link records the request and returns an error. Most callers should
// wrap this call in a function that returns a more useful result.
func (r *Links) Link(req *fuse.LinkRequest, old fs.Node, intr fs.Intr) (fs.Node, fuse.Error) {
	tmp := *req
	r.rec.RecordRequest(&tmp)
	return nil, fuse.EIO
}

// RecordedLink returns information about the Link request.
// If no request was seen, returns a zero value.
func (r *Links) RecordedLink() fuse.LinkRequest {
	val := r.rec.Recorded()
	if val == nil {
		return fuse.LinkRequest{}
	}
	return *(val.(*fuse.LinkRequest))
}

// Mknods records a Mknod request and its fields.
struct Mknods {
	rec RequestRecorder
}

var _ = fs.NodeMknoder(&Mknods{})

// Mknod records the request and returns an error. Most callers should
// wrap this call in a function that returns a more useful result.
func (r *Mknods) Mknod(req *fuse.MknodRequest, intr fs.Intr) (fs.Node, fuse.Error) {
	tmp := *req
	r.rec.RecordRequest(&tmp)
	return nil, fuse.EIO
}

// RecordedMknod returns information about the Mknod request.
// If no request was seen, returns a zero value.
func (r *Mknods) RecordedMknod() fuse.MknodRequest {
	val := r.rec.Recorded()
	if val == nil {
		return fuse.MknodRequest{}
	}
	return *(val.(*fuse.MknodRequest))
}

// Opens records a Open request and its fields.
struct Opens {
	rec RequestRecorder
}

var _ = fs.NodeOpener(&Opens{})

// Open records the request and returns an error. Most callers should
// wrap this call in a function that returns a more useful result.
func (r *Opens) Open(req *fuse.OpenRequest, resp *fuse.OpenResponse, intr fs.Intr) (fs.Handle, fuse.Error) {
	tmp := *req
	r.rec.RecordRequest(&tmp)
	return nil, fuse.EIO
}

// RecordedOpen returns information about the Open request.
// If no request was seen, returns a zero value.
func (r *Opens) RecordedOpen() fuse.OpenRequest {
	val := r.rec.Recorded()
	if val == nil {
		return fuse.OpenRequest{}
	}
	return *(val.(*fuse.OpenRequest))
}

// Getxattrs records a Getxattr request and its fields.
struct Getxattrs {
	rec RequestRecorder
}

var _ = fs.NodeGetxattrer(&Getxattrs{})

// Getxattr records the request and returns an error. Most callers should
// wrap this call in a function that returns a more useful result.
func (r *Getxattrs) Getxattr(req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse, intr fs.Intr) fuse.Error {
	tmp := *req
	r.rec.RecordRequest(&tmp)
	return fuse.ENODATA
}

// RecordedGetxattr returns information about the Getxattr request.
// If no request was seen, returns a zero value.
func (r *Getxattrs) RecordedGetxattr() fuse.GetxattrRequest {
	val := r.rec.Recorded()
	if val == nil {
		return fuse.GetxattrRequest{}
	}
	return *(val.(*fuse.GetxattrRequest))
}

// Listxattrs records a Listxattr request and its fields.
struct Listxattrs {
	rec RequestRecorder
}

var _ = fs.NodeListxattrer(&Listxattrs{})

// Listxattr records the request and returns an error. Most callers should
// wrap this call in a function that returns a more useful result.
func (r *Listxattrs) Listxattr(req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse, intr fs.Intr) fuse.Error {
	tmp := *req
	r.rec.RecordRequest(&tmp)
	return fuse.ENODATA
}

// RecordedListxattr returns information about the Listxattr request.
// If no request was seen, returns a zero value.
func (r *Listxattrs) RecordedListxattr() fuse.ListxattrRequest {
	val := r.rec.Recorded()
	if val == nil {
		return fuse.ListxattrRequest{}
	}
	return *(val.(*fuse.ListxattrRequest))
}

// Setxattrs records a Setxattr request and its fields.
struct Setxattrs {
	rec RequestRecorder
}

var _ = fs.NodeSetxattrer(&Setxattrs{})

// Setxattr records the request and returns an error. Most callers should
// wrap this call in a function that returns a more useful result.
func (r *Setxattrs) Setxattr(req *fuse.SetxattrRequest, intr fs.Intr) fuse.Error {
	tmp := *req
	r.rec.RecordRequest(&tmp)
	return nil
}

// RecordedSetxattr returns information about the Setxattr request.
// If no request was seen, returns a zero value.
func (r *Setxattrs) RecordedSetxattr() fuse.SetxattrRequest {
	val := r.rec.Recorded()
	if val == nil {
		return fuse.SetxattrRequest{}
	}
	return *(val.(*fuse.SetxattrRequest))
}

// Removexattrs records a Removexattr request and its fields.
struct Removexattrs {
	rec RequestRecorder
}

var _ = fs.NodeRemovexattrer(&Removexattrs{})

// Removexattr records the request and returns an error. Most callers should
// wrap this call in a function that returns a more useful result.
func (r *Removexattrs) Removexattr(req *fuse.RemovexattrRequest, intr fs.Intr) fuse.Error {
	tmp := *req
	r.rec.RecordRequest(&tmp)
	return nil
}

// RecordedRemovexattr returns information about the Removexattr request.
// If no request was seen, returns a zero value.
func (r *Removexattrs) RecordedRemovexattr() fuse.RemovexattrRequest {
	val := r.rec.Recorded()
	if val == nil {
		return fuse.RemovexattrRequest{}
	}
	return *(val.(*fuse.RemovexattrRequest))
}
